

package cn.shengchao.examstar.distribute.infrastrure.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import cn.shengchao.examstar.rpc.base.PageResult;
import cn.shengchao.examstar.rpc.distribute.enums.DistributeDataStatusEnum;
import cn.shengchao.examstar.rpc.distribute.enums.DistributeTypeEnum;
import cn.shengchao.examstar.rpc.uddsys.req.PlatformDistributeUserPageQuery;
import cn.shengchao.examstar.rpc.uddsys.resp.DistributeUserVo;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import cn.shengchao.examstar.distribute.infrastrure.mapper.DistributeNodeRoleChainMapper;
import cn.shengchao.examstar.distribute.infrastrure.po.DistributeNodeRoleChain;
import cn.shengchao.examstar.distribute.infrastrure.service.DistributeNodeRoleChainService;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@Service
public class DistributeNodeRoleChainServiceImpl extends ServiceImpl<DistributeNodeRoleChainMapper, DistributeNodeRoleChain> implements DistributeNodeRoleChainService {

    @Override
    public List<Long> selectSaleChainUserIds(Long userId) {
        if (userId == null) {
            return null;
        } else {
            String userIds = baseMapper.selectChainByDistributeUserAndType(userId, DistributeTypeEnum.TYPE_SALE
                .getType());
            if (userIds.indexOf(",") > 0) {
                List<String> list = Arrays.asList(userIds.split(","));
                return list.stream().filter(s -> !StrUtil.isBlank(s)).map(Long::parseLong).toList();
            } else {
                return List.of(userId);
            }
        }
    }

    @Override
    public List<Long> selectContentChainUserIds(Long userId) {
        if (userId == null) {
            return null;
        } else {
            String userIds = baseMapper.selectChainByDistributeUserAndType(userId, DistributeTypeEnum.TYPE_CTPD
                .getType());
            if (userIds.indexOf(",") > 0) {
                List<String> list = Arrays.asList(userIds.split(","));
                return list.stream().filter(s -> !StrUtil.isBlank(s)).map(Long::parseLong).toList();
            } else {
                return List.of(userId);
            }
        }
    }

    @Override
    public PageResult<DistributeUserVo> queryPlatformUserArchPage(PlatformDistributeUserPageQuery platformDistributeUserPageQuery) {
        Page<Object> page = PageHelper.startPage(platformDistributeUserPageQuery.getPage(), platformDistributeUserPageQuery
            .getSize());
        List<DistributeUserVo> selectPlatformRootDistributeUserList = baseMapper
            .selectPlatformRootDistributeUserList(platformDistributeUserPageQuery);
        return PageResult.of(page.getTotal(), selectPlatformRootDistributeUserList);
    }

    @Override
    public List<DistributeNodeRoleChain> selectDirectLowerNodeUserIds(Integer distributeType,Long userId) {
        return baseMapper.selectDirectLowerNodeUserIds(distributeType,userId);
    }

    @Override
    public void updateDspdLowNodeToNewNode(Long id, Long id1) {
        List<DistributeNodeRoleChain> list=baseMapper.selectList(Wrappers.lambdaQuery(DistributeNodeRoleChain.class)
                .eq(DistributeNodeRoleChain::getUpLevelNodeId,id)
                .eq(DistributeNodeRoleChain::getDataStatus, DistributeDataStatusEnum.STATUS_CURT.getStatus())
                .eq(DistributeNodeRoleChain::getDistributeType,DistributeTypeEnum.TYPE_SALE.getType())
        );
        if (CollUtil.isNotEmpty(list)){
            List<DistributeNodeRoleChain> updateNodeList=new ArrayList<>();
            baseMapper.update(
                    Wrappers.lambdaUpdate(DistributeNodeRoleChain.class)
                            .eq(DistributeNodeRoleChain::getUpLevelNodeId,id)
                            .eq(DistributeNodeRoleChain::getDataStatus, DistributeDataStatusEnum.STATUS_CURT.getStatus())
                            .eq(DistributeNodeRoleChain::getDistributeType,DistributeTypeEnum.TYPE_SALE.getType())
                            .set(DistributeNodeRoleChain::getDataStatus, DistributeDataStatusEnum.STATUS_HIS.getStatus())
            );
            for (DistributeNodeRoleChain distributeNodeRoleChain : list) {
                distributeNodeRoleChain.setId(null);
                distributeNodeRoleChain.setUpLevelNodeId(id1);
                updateNodeList.add(distributeNodeRoleChain);
            }
            saveBatch(updateNodeList);
        }
    }
}
